package com.cj.mobile.iabp.mvp.presenter;

import static com.cj.mobile.iabp.BuildConfig.IS_DEBUG_DATA;

import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.utils.RxUtils;
import com.cj.mobile.iabp.mvp.contract.ModifyLoginPwdContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


@ActivityScope
public class ModifyLoginPwdPresenter extends BasePresenter<ModifyLoginPwdContract.Model, ModifyLoginPwdContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public ModifyLoginPwdPresenter(ModifyLoginPwdContract.Model model, ModifyLoginPwdContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 修改密码
     */
    public void btnSubmitOnClick(String oldPwd, String newPwd, String confirmNewPwd) {
        // 用户输入有效性验证
        if (!checkInput(oldPwd, newPwd, confirmNewPwd)) {
            return;
        }

        if (IS_DEBUG_DATA) {
            mRootView.onUpdateSuccess();
        } else {
            mModel.changePassword(newPwd, oldPwd)
                    .compose(RxUtils.applySchedulers(mRootView))                               // 切换线程
                    .subscribe(new ErrorHandleSubscriber<Boolean>(mErrorHandler) {
                        @Override
                        public void onError(Throwable t) {
                            super.onError(t);
                            mRootView.onUpdateError();
                        }

                        @Override
                        public void onNext(Boolean commonResponse) {
                            mRootView.onUpdateSuccess();
                        }
                    });
        }
    }

    /**
     * 用户输入有效性验证
     *
     * @return 校验是否通过
     */
    private boolean checkInput(String oldPwd, String newPwd, String confirmNewPwd) {
        if ("".equals(oldPwd)) {
            mRootView.showMessage(ArmsUtils.getString(mRootView.getActivity().getApplicationContext(), R.string.modifyloginpwdactivity_oldpassword));
            return false;
        }
        if ("".equals(newPwd)) {
            mRootView.showMessage(ArmsUtils.getString(mRootView.getActivity().getApplicationContext(), R.string.modifyloginpwdactivity_newpassword));
            return false;
        }
        if (newPwd.length() < 6 || newPwd.length() > 32) {
            mRootView.showMessage(ArmsUtils.getString(mRootView.getActivity().getApplicationContext(), R.string.modifyloginpwdactivity_newpasswordlength));
            return false;
        }

        if ("".equals(confirmNewPwd)) {
            mRootView.showMessage(ArmsUtils.getString(mRootView.getActivity().getApplicationContext(), R.string.modifyloginpwdactivity_affirmpassword));
            return false;
        }
        if (confirmNewPwd.length() < 6 || confirmNewPwd.length() > 32) {
            mRootView.showMessage(ArmsUtils.getString(mRootView.getActivity().getApplicationContext(), R.string.modifyloginpwdactivity_affirmpasswordlength));
            return false;
        }

        if (!newPwd.equals(confirmNewPwd)) {
            mRootView.showMessage(ArmsUtils.getString(mRootView.getActivity().getApplicationContext(), R.string.modifyloginpwdactivity_twoinputpwdisnot));
            return false;
        }

        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
    }
}
